﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit.Electric.Common;

namespace Model
{
	public class CableTrayConduitBaseRoundBeam
	{
		public CableTrayConduitBaseRoundBeam(ExternalCommandData commandData, RoundBeamType type, CableTrayConduitBaseType cableTrayConduitBaseType)
		{
			this.commandData = commandData;
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			this.doc = activeUIDocument.Document;
			this.sel = activeUIDocument.Selection;
			this.roundBeamType = type;
			if (type == RoundBeamType.Lift)
			{
				this.title = "自动升降";
				this.roundBeamName = "升降";
			}
			else
			{
				this.title = "水平偏移";
				this.roundBeamName = "偏移";
			}
			if (cableTrayConduitBaseType == CableTrayConduitBaseType.CableTray)
			{
				this.prompt = "桥架";
				this.selectionFilter = new CableTrayFilter();
				return;
			}
			this.prompt = "线管";
			this.selectionFilter = new RevitFilter.ConduitFilter();
		}

		public void Excute()
		{
			IList<MEPCurve> list = new List<MEPCurve>();
			Transaction transaction = new Transaction(this.doc, "RoundBeam");
			XYZ item = null;
			try
			{
				transaction.Start();
				RevitVersionFuncs.ClearSelection(this.sel);
				CableTrayConduitBase cableTrayConduitBase;
				for (;;)
				{
					Reference reference = this.sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, this.selectionFilter, "请选择所要" + this.roundBeamName + "的" + this.prompt);
					cableTrayConduitBase = (this.doc.GetElement(reference) as CableTrayConduitBase);
					if (!new HYCableTray<CableTrayConduitBase>(cableTrayConduitBase).IsZAxis())
					{
						break;
					}
					TaskDialog.Show("盈建科提示", "请选择水平" + this.prompt + "。");
				}
				list.Add(cableTrayConduitBase);
				MEPCurve mepcurve;
				do
				{
					Reference reference = this.sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new RevitFilter.OffsetDeviceFilter(), "请选择参照的桥架、线管、风管或水管");
					mepcurve = (this.doc.GetElement(reference) as MEPCurve);
				}
				while (list[0].Id == mepcurve.Id);
				list.Add(mepcurve);
				if (!RoundBeanEntity.IsElementIntersectsElement(list[0], list[1]))
				{
					transaction.RollBack();
					TaskDialog.Show("盈建科提示", "请选择碰撞的两个设备。");
				}
				else
				{
					if (this.roundBeamType == RoundBeamType.Offset)
					{
						item = this.sel.PickPoint("请选择避让方向");
					}
					RoundBeanEntity.GetHeightOrDiameter(list[1]);
					double widthOrDiameter = RoundBeanEntity.GetWidthOrDiameter(list[1]);
					using (RoundBeam roundBeam = new RoundBeam(this.title, widthOrDiameter))
					{
						DialogResult dialogResult = roundBeam.ShowDialog();
						if (DialogResult.OK == dialogResult)
						{
							double angle = 1.0471975511965976;
							if (roundBeam.rdo30.Checked)
							{
								angle = 0.52359877559829882;
							}
							else if (roundBeam.rdo45.Checked)
							{
								angle = 0.78539816339744828;
							}
							else if (roundBeam.rdo60.Checked)
							{
								angle = 1.0471975511965976;
							}
							else if (roundBeam.rdo90.Checked)
							{
								angle = Math.PI*.5;
							}
							else if (roundBeam.rdoAny.Checked)
							{
								angle = Convert.ToDouble(roundBeam.hytxtAngle.Text.Trim()) * Math.PI / 180.0;
							}
							double offSet = AssistFunc.mmToFeet(Convert.ToDouble(roundBeam.txtOffSet.Text.Trim()));
							double space = AssistFunc.mmToFeet(Convert.ToDouble(roundBeam.txtSpace.Text.Trim()));
							IList<XYZ> list2 = new List<XYZ>();
							list2 = RoundBeanEntity.GetListXYZ(list[0] as CableTrayConduitBase, mepcurve, space);
							list2.Add(item);
							if (new HYCableTrayConduitRoundBeam().CableTrayConduitRoundBeam(list, list2, offSet, angle, this.roundBeamType))
							{
								transaction.Commit();
							}
							else
							{
								transaction.RollBack();
							}
						}
						else
						{
							transaction.RollBack();
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				transaction.RollBack();
			}
			catch
			{
				transaction.RollBack();
				TaskDialog.Show("盈建科提示", string.Concat(new string[]
				{
					"要",
					this.roundBeamName,
					"的",
					this.prompt,
					"太短。"
				}));
			}
		}

		private Document doc;

		private Selection sel;

		private ExternalCommandData commandData;

		private RoundBeamType roundBeamType;

		private string title = "";

		private string prompt = "";

		private string roundBeamName = "";

		private ISelectionFilter selectionFilter;
	}
}
